﻿using System;
using System.Threading;

public class AlarmClock
{
    public event EventHandler Tick;
    public event EventHandler Alarm;
    public event EventHandler Stopped; 
    public int AlarmTime { get; set; }
    public int currentTime;
    private bool isRunning;  
    public AlarmClock(int alarmTime)
    {
        AlarmTime = alarmTime;
        currentTime = 0;
        isRunning = true;
    }

    public void Start()
    {
        while (isRunning && currentTime <= AlarmTime)
        {
            Thread.Sleep(1000);
            currentTime++;
            Tick?.Invoke(this, EventArgs.Empty);

            if (currentTime == AlarmTime)
            {
                Alarm?.Invoke(this, EventArgs.Empty);
            }
        }
        Stopped?.Invoke(this, EventArgs.Empty); 
    }

    public void Stop()
    {
        isRunning = false;
    }
}

class Program
{
    static void Main()
    {
        var alarmClock = new AlarmClock(10);  
        alarmClock.Tick += (sender, e) =>
            Console.WriteLine($"Tick，当前: {((AlarmClock)sender).currentTime}s");

        alarmClock.Alarm += (sender, e) =>
            Console.WriteLine("Alarm");

        alarmClock.Stopped += (sender, e) =>
            Console.WriteLine("闹钟停止.");

        Thread alarmThread = new Thread(alarmClock.Start);
        alarmThread.Start();

        Console.WriteLine("闹钟开启，按任意键停止.");
        Console.ReadKey();  
        // 用户按下任意键后停止闹钟
        alarmClock.Stop();
        alarmThread.Join(); 
        Console.ReadKey();
    }
}